import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import Compression from 'vite-plugin-compression';
import { resolve } from 'path';

export default defineConfig(({ command }) => {
  const isBuild = command === 'build';
  const alias = {
    '@/': resolve('src') + '/'
  };
  const plugins = [vue()];

  if (isBuild) {
    // gzip压缩
    plugins.push(
      Compression({
        disable: !isBuild,
        threshold: 10240,
        algorithm: 'gzip',
        ext: '.gz'
      })
    );
  }

  return {
    resolve: { alias },
    // 配置前端服务地址和端口,默认端口是5173
    server: {
      host: '0.0.0.0',
      port: 5172,
      // 是否开启 https
      https: false,
      // 开发服务器启动时，自动在浏览器中打开应用程序 open: '/docs/index.html'
      open: false
    },
    plugins,
    build: {
      target: 'chrome63',
      chunkSizeWarningLimit: 2000
    }
  };
});
